//
//  BGMyTenderTableViewCell.m
//  bigMall
//
//  Created by LTOVE on 16/8/18.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGMyTenderTableViewCell.h"
#import "BGMyTenderModel.h"
#import "BGDateFormaTool.h"
#define lineMagin 5
static NSString *const reuseIdentify = @"cell";
@interface BGMyTenderTableViewCell (){
//    NSString *statusStr;
}
@property (nonatomic,copy) NSString *statusStr;
@end
@implementation BGMyTenderTableViewCell

- (NSString *)statusStr
{
    if (!_statusStr) {
        _statusStr = [NSString string];
    }
    return _statusStr;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    BGMyTenderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentify];
    if (!cell) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentify ];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)setModel:(BGMyTenderModel *)model
{
    //创建视图
    [self setUpAllSubviewsWith:model];
}

- (void)setUpAllSubviewsWith:(BGMyTenderModel *)model
{
    //内容视图
    UIView *ContentView = [[UIView alloc]init];
    ContentView.width = screenW;
    ContentView.backgroundColor = lightwhite_;
//    UIView *btnView;
    [self SetUpBtnsWith:nil status:model.status isBack:model.is_backed publishType:model.publish_type tag:model.tag];
    
    //cell分割线
    UIView *cellLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, BGMaginButtom)];
    cellLineView.backgroundColor = dockwhite_;
    [ContentView addSubview:cellLineView];
    //标题
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = model.title;
    titleLabel.font = Font34;
    titleLabel.textColor = textBlackColor;
    [titleLabel sizeToFit];
    titleLabel.x = BGMaginLeft;
    titleLabel.y = CGRectGetMaxY(cellLineView.frame) + lineMagin * 2;
    [ContentView addSubview:titleLabel];
    //右边副标题
    UILabel *secTitleLabel = [[UILabel alloc]init];
    secTitleLabel.text = self.statusStr;
    secTitleLabel.font = Font30;
    secTitleLabel.textColor = read_;
    [secTitleLabel sizeToFit];
    secTitleLabel.centerY = titleLabel.centerY;
    secTitleLabel.x = screenW - BGMaginLeft - secTitleLabel.width;
    [ContentView addSubview:secTitleLabel];
    //分割线
    UIView *linView1 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(titleLabel.frame) + lineMagin * 2, screenW, 1)];
    linView1.backgroundColor = gray_;
    [ContentView addSubview:linView1];
    //单价
    UILabel *price = [[UILabel alloc]init];
    NSString *priceStr = [NSString stringWithFormat:@"单价:%@",model.price];
    NSDictionary *dic = @{
                          NSFontAttributeName:Font30,
                          NSForegroundColorAttributeName:textBlackColor,
                          };
    NSMutableAttributedString *attrSr = [[NSMutableAttributedString alloc]initWithString:priceStr attributes:dic];
    NSRange range = [priceStr rangeOfString:@":"];
    [attrSr addAttribute:NSForegroundColorAttributeName value:read_ range:NSMakeRange(range.location + 1, priceStr.length - 1 - range.location)];
    price.attributedText = attrSr;
//    price.textColor =
    [price sizeToFit];
    price.x = titleLabel.x;
    price.y = lineMagin + CGRectGetMaxY(linView1.frame);
    [ContentView addSubview:price];
    //数量
    UILabel *numLabel = [[UILabel alloc]init];
    numLabel.text = [NSString stringWithFormat:@"数量(%@):%@",model.unit,model.number];
    numLabel.textColor = textBlackColor;
    numLabel.font = Font30;
    [numLabel sizeToFit];
    numLabel.x = titleLabel.x;
    numLabel.y = lineMagin + CGRectGetMaxY(price.frame);
    [ContentView addSubview:numLabel];
    //发布时间
    UILabel *addTime = [[UILabel alloc]init];
    NSString *dateForm = [BGDateFormaTool dateFormateWithDateStr:model.add_time];
    addTime.text = [NSString stringWithFormat:@"发布时间:%@",dateForm];
    addTime.textColor = textBlackColor;
    addTime.font = Font30;
    [addTime sizeToFit];
    addTime.x = titleLabel.x;
    addTime.y = lineMagin + CGRectGetMaxY(numLabel.frame);
    [ContentView addSubview:addTime];
    //分割线2
    UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(addTime.frame) + lineMagin, screenW, 1)];
    lineView2.backgroundColor = gray_;
    [ContentView addSubview:lineView2];
    //底部button
    UIView *btnView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(lineView2.frame), screenW, 4 * lineMagin + titleLabel.height)];
    [self SetUpBtnsWith:btnView status:model.status isBack:model.is_backed publishType:model.publish_type tag:model.tag];
    [ContentView addSubview:btnView];
    CGFloat heigh = cellLineView.height + lineMagin + titleLabel.height + lineMagin + linView1.height + lineMagin * 3 + price.height + lineMagin + numLabel.height + lineMagin + addTime.height + lineMagin + lineView2.height + btnView.height ;
    ContentView.height = heigh;
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    linView1.alpha = 0.5;
    lineView2.alpha = 0.5;
    [self.contentView addSubview:ContentView];
}
#pragma mark - ⬇️脱货保证金 下架  投标记录按钮
- (void)SetUpBtnsWith:(UIView *)view
               status:(NSString *)status
               isBack:(NSString *)isBack
          publishType:(NSString *)publishType
                  tag:(NSInteger)tag

{
    
//    2 3 退钱
//    -1 0 1 下架
//    1 3 4 投标记录
    NSString *btnStr = [self btnStrWithStatus:status isBack:isBack publishType:publishType];
    [self setUpTwoBtnWith:view oneBtnTitle:@"投标记录" twoBtntitle:btnStr tag:tag];
    //投标记录
//    switch ([status intValue]) {
//        case -1:case 0:
//            //下架
//            [self setBtnWithView:view title:@"下架" tag:tag];
//            break;
//        case 1:
//            //投标记录 下架
//            [self setUpTwoBtnWith:view oneBtnTitle:@"投标记录" twoBtntitle:@"下架" tag:tag];
//            break;
//        case 2:
//            //退款
//            [self setBtnWithView:view title:@"退还保证金" tag:tag];
//            break;
//        case 3:
//            //投标记录  退款
//            [self setUpTwoBtnWith:view oneBtnTitle:@"投标记录" twoBtntitle:@"退还保证金" tag:tag];
//            break;
//        case 4:
//            //投标记录
//            [self setBtnWithView:view title:@"投标记录" tag:tag];
//            break;
//        default:
//            break;
//    }
    
}
/**
 *  判断状态
 *
 *  @param status      <#status description#>
 *  @param isBack      <#isBack description#>
 *  @param publishType <#publishType description#>
 *
 *  @return <#return value description#>
 */
- (NSString *)btnStrWithStatus:(NSString *)status
                        isBack:(NSString *)isBack
                   publishType:(NSString *)publishType
{
    NSString *str = [NSString string];
    
    if ([status isEqualToString:@"-1"]) {
        str = @"下架";
        self.statusStr = @"准备资料";
    }else if ([status isEqualToString:@"0"]){
        str = @"下架";
        self.statusStr = @"待审核";
    }else if ([status isEqualToString:@"1"]){
        str = @"下架";
        self.statusStr = @"审核通过";
    }else if ([status isEqualToString:@"2"]){
        str = @"下架";
        self.statusStr = @"审核失败";
    }else if ([status isEqualToString:@"3"]){
        if ([publishType isEqualToString:@"2"] && ![isBack isEqualToString:@"1"]) {
            str = @"退还保证金";
        }else{
            str = @"删除";
        }
        self.statusStr = @"已下架";
    }else if ([status isEqualToString:@"4"]){
        str = @"下架";
        self.statusStr = @"售空";
    }
    
    
    return str;
}
/**
 *  添加两个btn
 *
 *  @param view     view description
 *  @param onetitle onetitle description
 *  @param twoTitle twoTitle description
 */
- (void)setUpTwoBtnWith:(UIView *)view
            oneBtnTitle:(NSString *)onetitle
            twoBtntitle:(NSString *)twoTitle
                    tag:(NSInteger)tag
{
    UIButton *bidListBtn = [self btnWithTitle:onetitle];
//    bidListBtn.hidden = YES;
    bidListBtn.x = screenW - BGMaginLeft - bidListBtn.width;
    bidListBtn.centerY = view.height * 0.5;
    [view addSubview:bidListBtn];
    
    //下架
    UIButton *getOutBtn = [self btnWithTitle:twoTitle];
    getOutBtn.centerY = bidListBtn.centerY;
    getOutBtn.x = bidListBtn.x - lineMagin - getOutBtn.width;
    [view addSubview:getOutBtn];
    bidListBtn.tag = tag;
    getOutBtn.tag = tag;
    //退还保证金

}
/**
 *  添加一个btn
 *
 *  @param view  view description
 *  @param title title description
 */
- (void)setBtnWithView:(UIView *)view
                 title:(NSString *)title
                   tag:(NSInteger)tag
{
    UIButton *bidListBtn = [self btnWithTitle:title];
    //    bidListBtn.hidden = YES;
    bidListBtn.tag = tag;
    bidListBtn.x = screenW - BGMaginLeft - bidListBtn.width;
    bidListBtn.centerY = view.height * 0.5;
    [view addSubview:bidListBtn];
}
- (UIButton *)btnWithTitle:(NSString *)title
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:lightwhite_ forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageWithColor:read_ size:CGSizeMake(80, 30)] forState:UIControlStateNormal];
    [btn setFont:Font30];
    btn.size = CGSizeMake(80, 30);
    btn.layer.cornerRadius = 5;
    btn.layer.masksToBounds = YES;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}
- (void)btnClick:(UIButton *)btn
{
    NSLog(@"%@%ld",btn.titleLabel.text,(long)btn.tag);
    if ([_delegate respondsToSelector:@selector(btnClick:)]) {
        [_delegate btnClick:btn];
    }

}
@end
